package com.davidhigenbottam.HeatMap.client;

import com.davidhigenbottam.HeatMap.shared.Institution;
import com.davidhigenbottam.HeatMap.shared.Result;
import com.davidhigenbottam.HeatMap.shared.Results;
import com.google.gwt.core.client.JavaScriptObject;


public class InstitutionsLayer {
	
	Results results;
	JavaScriptObject jsMap;
	JavaScriptObject jsMarkerArray;
	boolean visible= true;
	
	public InstitutionsLayer(Results results,JavaScriptObject jsMap) {
		super();
		this.results = results;
		this.jsMap = jsMap;
	}

	public void setVisible(boolean b) {
		if(b)
			show();
		else
			hide();
		
		this.visible= b;
		
	}


	private void hide() {
		
		nativeHideOverlays();
	}

	private void show() {
		if(jsMarkerArray==null){
			jsMarkerArray= nativeCreateMarkerArray();
			for(Result r : this.results.getInstitutions()){
				Institution i = (Institution) r;
				if(i.isGeocoded()){
					String iconUrl = i.isPrivate() ? "http://maps.google.com/mapfiles/kml/paddle/blu-circle.png"
							: "http://maps.google.com/mapfiles/kml/paddle/red-circle.png";
					
					int size= (int) ((this.results.getInstitutionScale(i)*48) + 16);
					nativeAddMarker(i.getGeocode(), i.getName(), i.getUrl(), i.getValueDescription(),iconUrl,size);
				}
			}
		}
		else
			nativeShowOverlays();
	}

	public boolean isHidden() {
		return this.visible;
	}

	
	public native JavaScriptObject nativeCreateMarkerArray() /*-{
	
	var markersArray = [];
	return markersArray;
	
}-*/;		
	
	
	public native void nativeAddMarker(String geocode, String name, String url, String value,String iconUrl, int size) /*-{
	
		var latlngArr = geocode.split(',');
		//$wnd.alert('latlngArr:'+latlngArr);
		//$wnd.alert('latlngArr[0]:'+latlngArr[0]);
		//$wnd.alert('latlngArr[1]:'+latlngArr[1]);
		
		var latlng = new $wnd.google.maps.LatLng(latlngArr[1],latlngArr[0]);
		
		//var latlng = new $wnd.google.maps.LatLng(-2.51336,118.034121);
		
		var myinfowindow = new $wnd.google.maps.InfoWindow({
		    content: "<a href=\""+url+"\" target=\"_blank\">"+name+"</a><br/>Total:"+value
		    //content: 'this is a test'
		  });
		 
		var markerImage=  new $wnd.google.maps.MarkerImage(
			  iconUrl, 
		      null, 
		      null, 
		      null, 
		      new $wnd.google.maps.Size(size, size)); 
		      
		var title= name+':'+value; 
		
		var marker = new $wnd.google.maps.Marker({
		    position: latlng,
		    title: title,
		    icon:markerImage,
		    zIndex: (400-size)
		  });
		  //$wnd.alert('about to add marker to map:'+title);
		  marker.setMap(this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMap);
		  
		  $wnd.google.maps.event.addListener(marker, 'click', function() {
		  	//$wnd.alert('about to open info window:marker:getPosition:'+ marker.getPosition().toString());
		  	//myinfowindow.setContent('What a load of bollocks!');
		    myinfowindow.open(marker.getMap(),marker);
		    //$wnd.alert('infowindow.getPosition:'+ myinfowindow.getPosition().toString());
//		    var anotherinfowindow = new $wnd.google.maps.InfoWindow({
//		    //content: name+"<br/>"+url+"<br/>Total:"+value
//		    content: 'anotherinfowindow this is a test'
//		    });
//		    anotherinfowindow.open(marker.getMap(),marker);
		  	
		  });  
		  this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray.push(marker);  
		  
		  

}-*/;	
	
	
	public native void nativeHideOverlays() /*-{
		  if (this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray) {
		  	var marker;
		  	for (var i=0; i<this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray.length; i++) {
		      marker= this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray[i];
		      if(marker)
		      	marker.setMap(null);
		    }
		  }	
}-*/;	

	public native void nativeShowOverlays() /*-{
	  if (this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray) {
	  		var marker;
		    for (var i=0; i<this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray.length; i++) {
		      marker= this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMarkerArray[i];
		      if(marker)
		      	marker.setMap(this.@com.davidhigenbottam.HeatMap.client.InstitutionsLayer::jsMap);
		    }
		  }
}-*/;		
	


		
}
